<?php

switch (@$URL[1]) {
    case '':
    default:
        
        $_page_content = $current_page->fetch( 'project.tpl' );
        
        break;
    case 'create':
        
        $coins_all = $Coin->get_all();
        $current_page->assign( 'coins', $coins_all );
        
        $_page_content = $current_page->fetch( 'project_create.tpl' );
        
        break;
    
    case 'create-action':
        
        $name = filter_cross( $_POST['project_name'] );
        $budget = $_POST['project_budget'];
        $description = filter_cross( nl2br( $_POST['project_description'] ) );
        $coin = (int)$_POST['project_coin'];
        
        $project_added = $Project->add( array(
            'name' => $name,
            'budget' => $budget,
            'description' => $description,
            'coin' => $coin,
            'user' => $User->get_id()
        ) );
        
        if ( $project_added )
        {
            // Process intervals
            $intervals_start = array();
            $intervals_end = array();
            foreach( $_POST['interval_start'] as $i_start )
            {
                $intervals_start[] = $i_start;
            }
            foreach( $_POST['interval_end'] as $i_end )
            {
                $intervals_end[] = $i_end;
            }
            
            for( $i = 0 ; $i < count( $intervals_start ) ; $i++ )
            {
                $Project_intervals->add( array(
                    'project' => $project_added,
                    'date_start' => $intervals_start[$i],
                    'date_end' => $intervals_end[$i],
                ) );
            }
            
        }
        
        $current_page->assign( 'project_added', $project_added );
        
        $_page_content = $current_page->fetch( 'project_create_action.tpl' );
        
        break;
        
    case 'list':
        
        $projects = $Project->get_all();
        
        $current_page->assign( 'projects', $projects );
        
        $_page_content = $current_page->fetch( 'project_list.tpl' );
        
        break;
    
    case 'details':
        
        $project_id = (int)@$URL[2];
        $project = $Project->find_by( 'id', $project_id );
        
        break;
}
?>